package gs.wicket.app.product;

import java.text.NumberFormat;
import java.util.Currency;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import gs.wicket.app.GSApp;
import gs.wicket.app.base.BasePage;
import gs.wicket.app.session.Cart;
import gs.wicket.app.session.GSSession;

public class ProductsPage extends BasePage{
	public ProductsPage() {
	    super();
	    
	    add(new ListView("cart", new PropertyModel(this, "cart.products")) {
	    	
//			private static final long serialVersionUID = 1L;

			@Override
		      protected void populateItem(ListItem item) {
		        Product product = (Product) item.getModelObject();
		        item.add(new Label("name", product.getName()));
		        item.add(new Label("price", product.getPrice() + "  €"));
		        item.add(new Link("remove", item.getModel()) {		         
//					private static final long serialVersionUID = 1L;				

				@Override
		          public void onClick() {
		            Product selected = (Product) getModelObject();
		            getCart().getProducts().remove(selected);
		          }
		        });
		      }
		    });
	    
		    add(new Label("total", new Model() {
		      
				private static final long serialVersionUID = 1L;

			@Override
		      public Object getObject() {
		    	    NumberFormat nf = NumberFormat.getCurrencyInstance();
					nf.setCurrency(Currency.getInstance("EUR"));
					return nf.format(getCart().getTotal());
		      }
		    }));
		    
		    Link checkOut = new Link("checkout"){
		    	{
					add(new AttributeModifier("value", new Model(getLangProperty("products.page.order"))));
				}
				private static final long serialVersionUID = 1L;
				@Override
			      public void onClick() {
			    	  if(getCart().getProducts().size()==0)
							info("Vaša korpa je prazna!");
						else
							setResponsePage(Checkout.class);
					}
			      @Override
			      public boolean isVisible() {
			        return !getCart().getProducts().isEmpty();
			      }
			    }; 
			   add(checkOut);
			   add(new Label("products.page.title", getLangProperty("products.page.title")));
			   add(new Label("products.page.total", getLangProperty("products.page.total")));
}

public GSSession getGSSession() {
    return (GSSession) getSession();
  }

  public Cart getCart() {
    return getGSSession().getCart();
  }

  public List<Product> getProducts() {
    return GSApp.get().getProducts(((GSSession) getSession()).getLanguage());
  }
  public List<Product> getWatchProducts() {
	    return GSApp.get().getWatchProducts(((GSSession) getSession()).getLanguage());
	  }
  public List<Product> getParfumProducts() {
		return GSApp.getParfumProducts(((GSSession) getSession()).getLanguage());
	  }
  public List<Product> getDrinkProducts() {
	    return GSApp.get().getDrinkProducts(((GSSession) getSession()).getLanguage());
	  }
  public List<Product> getJewelryProducts() {
	    return GSApp.get().getJewelryProducts(((GSSession) getSession()).getLanguage());
	  }
}
